package com.xxxy.ljh.myapplication.activity.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.xxxy.ljh.myapplication.Dao.UserDao;
import com.xxxy.ljh.myapplication.DaoImpl.UserDaoImpl;
import com.xxxy.ljh.myapplication.Manager.UserManager;
import com.xxxy.ljh.myapplication.R;
import com.xxxy.ljh.myapplication.model.User;

public class LoginActivity extends AppCompatActivity {

    private EditText etUsername, etPassword;
    private Button btnLogin, btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        initViews();
        setListeners();
    }

    private void initViews() {
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btnLogin = findViewById(R.id.btn_login);
        btnRegister = findViewById(R.id.btn_register);

    }

    private void setListeners() {
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                performLogin();
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到注册页面
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    private void performLogin() {
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        UserDao userDao = new UserDaoImpl(this);

         if (username.isEmpty()) {
             etUsername.setError("请输入用户名");
             etUsername.requestFocus();
             return;
         }

         if (password.isEmpty()) {
             etPassword.setError("请输入密码");
             etPassword.requestFocus();
             return;
         }

        if(username.equals("SuperAdmin")&&password.equals("SuperAdmin")){
            // 超级管理员权限
            Toast.makeText(this, "超级管理员登录成功", Toast.LENGTH_SHORT).show();
            Intent resultIntent = new Intent();
            resultIntent.putExtra("logged_in_user", username);
            resultIntent.putExtra("user_role", 3); // 3表示超级管理员
            setResult(RESULT_OK, resultIntent);
            finish();
        } else {
            if (userDao.login(username, password)) {

                // 登录成功，获取用户详细信息以判断权限
                // TODO: 这里可以根据实际需求从数据库获取用户角色信息
                int userRole = userDao.getUserRoleFromDatabase(username); // 需要实现此方法
                User user = userDao.getUserByUsername(username); // 获取完整用户信息

                UserManager userManager = UserManager.getInstance(this);
                userManager.loginUser(user); // 这里会设置 currentUser
                // 权限判断
                if (userRole == 0) {
                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                } else if (userRole == 1) {
                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                } else if (userRole == 2) {
                    Toast.makeText(this, "管理员登录成功", Toast.LENGTH_SHORT).show();
                }
                Intent resultIntent = new Intent();
                resultIntent.putExtra("logged_in_user", username);
                resultIntent.putExtra("user_role", userRole);
                setResult(RESULT_OK, resultIntent);
                finish(); // 关闭登录页面
            } else {
                // 登录失败
                Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }
        }
     }



}
